﻿using NthDownload.Common.ViewModels;
using NthDownload.Contracts.Plugins.Downloads;
using System.ComponentModel.Composition;
using System.Collections.ObjectModel;
using NthDownload.Contracts;

namespace NthDownload.ViewModels
{
    public class DownloadPluginSelectorViewModel : ViewModelScreenBase
    {
        #region Class Members

        private IDownloadPlugin _selectedPlugin;
        private ObservableCollection<IDownloadPlugin> _downloadPlugins;

        #endregion Class Members

        #region Constructor

        public DownloadPluginSelectorViewModel()
        {
            //
            // Set Defaults
            //
            SelectedPlugin = null;
        }

        #endregion Constructor

        #region Properties

        /// <summary>
        /// The currently selected plugin
        /// </summary>
        public IDownloadPlugin SelectedPlugin
        {
            get
            {
                return _selectedPlugin;
            }
            set
            {
                _selectedPlugin = value;
                NotifyOfPropertyChange(() => SelectedPlugin);
            }
        }

        /// <summary>
        /// The available download plugins
        /// </summary>
        [ImportMany(typeof(IDownloadPlugin), AllowRecomposition = true)]
        public ObservableCollection<IDownloadPlugin> DownloadPlugins
        {
            get
            {
                return _downloadPlugins;
            }
            set
            {
                _downloadPlugins = value;
                NotifyOfPropertyChange(() => DownloadPlugins);
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Set the selected plugin then closes the dialog with a select result
        /// </summary>
        public void Select(IDownloadPlugin plugin)
        {
            if (plugin == null)
            {
                return;
            }

            SelectedPlugin = plugin;

            Result = ViewModelResults.Select;
            TryClose();
        }

        /// <summary>
        /// Closes the dialog with a cancel result
        /// </summary>
        public void Cancel()
        {
            Result = ViewModelResults.Cancel;
            TryClose();
        }

        #endregion Methods
    }
}
